package cn.tedu.sp02.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.service.ItemService;

import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class ItemController {
    @Autowired
    private ItemService service;
    //获取商品数据
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {

        List<Item> items = service.getItems(orderId);
        //添加随机延迟代码
        if(Math.random()<0.1){
            //延迟时长随机

            int i = new Random().nextInt(5000);
            log.info("请求超时:"+i);
            Thread.sleep(i);

        }


        return JsonResult.ok().data(items);
    }

    /**
     * @requestBody
     * 完整的接收请求协议体的数据，转成list<Item>
     * @param list
     * @return
     */
    //减少商品库存
@PostMapping("/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> list){
       service.decreaseNumber(list);
      return JsonResult.ok().msg("减少商品库存成功");
    }


    //象浏览器放回一个空数据，不会报错
    @GetMapping("/favicon.ico")
    public void ico(){


    }


}
